﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Design
{
    public partial class EleAddForm : Form
    {
        CXmlCtrl insXmlCtrl = null;
        CHtmlParsing insHtmlParsing = null;
        public string m_sHtml = "";

        public EleAddForm(int iSelctItem)
        {
            InitializeComponent();

            insXmlCtrl = CXmlCtrl.GetInstance();
            insHtmlParsing = CHtmlParsing.GetInstance();

            m_cbEleType.Items.Add("Label");
            m_cbEleType.Items.Add("TextBox");
            m_cbEleType.Items.Add("CombBox");
           
            m_cbEleType.SelectedIndex = iSelctItem;
        }

        private ElementType TransformCtrlType(string strCtrl)
        {
            if (0 == strCtrl.CompareTo("TextBox"))
            {
                return ElementType.ELE_TEXTBOX;
            }

            if (0 == strCtrl.CompareTo("CombBox"))
            {
                return ElementType.ELE_COMBBOX;
            }

            if (0 == strCtrl.CompareTo("Label"))
            {
                return ElementType.ELE_LABEL;
            }

            return ElementType.ELE_UNKNOW;
        }

        private void m_cbEleType_SelectedIndexChanged(object sender, EventArgs e)
        {
            string strTxt = m_cbEleType.SelectedItem.ToString();
            string strId = "";
            ErrorCode errCode = ErrorCode.ERR_OK;
            ElementType eleType = ElementType.ELE_UNKNOW;

            eleType = TransformCtrlType(strTxt);
            errCode = insXmlCtrl.GetEleCtrlId(eleType, ref strId);

            if (ErrorCode.ERR_OK != errCode)
            {
                MessageBox.Show(errCode.ToString());
            }

            switch (eleType)
            {
                case ElementType.ELE_TEXTBOX:
                {
                    strId = string.Format("TEXTBOX_{0}", strId);                    

                    break;
                }
                case ElementType.ELE_COMBBOX:
                {
                    strId = string.Format("COMBBOX_{0}", strId);

                    break;
                }
                case ElementType.ELE_LABEL:
                {
                    strId = string.Format("LABEL_{0}", strId);

                    break;
                }
                default:
                {
                    break;
                } 
            }

            m_txtEleId.Text = strId;
        }

        private void m_btnOk_Click(object sender, EventArgs e)
        {
            ElementType eleType = ElementType.ELE_UNKNOW;
            ErrorCode errCode = ErrorCode.ERR_OK;

            if (m_txtEleText.Text.Trim().Length == 0)
            {
                MessageBox.Show("Please input element text!");
                return;
            }

            if (m_txtEleId.Text.Trim().Length == 0)
            {
                MessageBox.Show("Please input element ID!");
                return;
            }

            string strTxt = m_cbEleType.SelectedItem.ToString();
            eleType = TransformCtrlType(strTxt);

            if (ElementType.ELE_UNKNOW == eleType)
            {
                MessageBox.Show("Element type unknow!");
                return;
            }

            errCode = insXmlCtrl.SetEleCtrlId(eleType, m_txtEleId.Text);
            errCode = insHtmlParsing.ParsingToHtml(eleType, m_txtEleId.Text, ref m_sHtml, m_txtEleText.Text);

            this.DialogResult = DialogResult.OK;
        }
    }
}
